/*
 * Copyright (c) 2000, Thomas W. Christopher and George K. Thiruvathukal
 * 
 * Java and High Performance Computing (JHPC) Organzization Tools of Computing
 * LLC
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * The names Java and High-Performance Computing (JHPC) Organization, Tools of
 * Computing LLC, and/or the names of its contributors may not be used to
 * endorse or promote products derived from this software without specific prior
 * written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * This license is based on version 2 of the BSD license. For more information
 * on Open Source licenses, please visit http://opensource.org.
 */

package info.jhpc.message2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MessageClient extends Thread {
	Socket socket;

	DataOutputStream out;

	DataInputStream in;

	public MessageClient(String host, int port) throws IOException {
		socket = new Socket(host, port);
		out = new DataOutputStream(socket.getOutputStream());
		in = new DataInputStream(socket.getInputStream());
	}

	public Message call(Message message) {
		try {
			message.encode(out);
		} catch (Exception e) {
			System.err.println("MessageClient: Call (to) failure: " + e);
			return null;
		}

		try {
			Message m = new Message();
			m.decode(in);
			return m;
		} catch (Exception e) {
			System.err.println("MessageClient: Call (from) failure: " + e);
			return new Message();
		}
	}

	public void disconnect() {
		Message m = new Message();
		m.setType(0);
		m.setParam("$disconnect", "$disconnect");
		call(m);
		try {
			socket.close();
		} catch (Exception e) {
			System.err.println("ungraceful disconnect on client " + e);
		}
	}
}